# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：baidu_url.py
#日期：2019-11-08
#备注：Python利用百度url
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import requests
from pycacho.cachobase.deal_json import dict_to_json


headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/json',
        }

headers1 = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/x-www-form-urlencoded',
        }



client_id = '9mYSCNj1iFIvUsfU7vNfVjz9'
client_secret = 'GjCQdgC1FCs2eNVPdDHY1DGAF4hEeyvZ'
access_token = '24.55052d60cb1eaad259b013ec81f4bec4.2592000.1575858669.282335-17732245'

url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials'
key = '&client_id=%s&client_secret=%s' %(client_id,client_secret)


def apply():
    fu_url = url + key
    #print(fu_url)
    req1 = requests.get(fu_url, headers=headers)
    data = req1.json()
    #print(data['access_token'])
    return data['access_token']


# 根据用户输入的命题关键词自动生成一副春联，包括上联、下联和横批。
# first春联上联：成7-9个字的上联；second春联下联：生成7-9个字的春联下联；center春联横批：生成4个字的春联横批
def create_couplet():
    body = {
        "text": "胡翠红",
        "index": 1          # 默认为0，+1换一个
    }
    url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/couplets'
    c_url = url + '?access_token=%s' % (access_token)
    #print(c_url)
    req1 = requests.post(c_url, json=body,headers=headers)
    data = req1.json()
    #print(type(data))
    dict_to_json(data)


# 根据用户输入的命题关键词自动生成一首7言绝句。
def create_poem():
    body = {
        "text": "胡翠红",
        "index": 1
    }
    url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/poem'
    c_url = url + '?access_token=%s' % (access_token)
    req1 = requests.post(c_url, json=body,headers=headers)
    try:
        data = req1.json()['poem'][0]['content'].replace('\t','\n')
        print(data)
    except Exception as f:
        print(str(f))



if __name__ == '__main__':
    #access_token = apply()
    create_couplet()
    #create_poem()
